<?php

class Admin_SettingsController extends Zend_Controller_Action
{
    protected $_configTable;

    public function init()
    {
        $this->_configTable = new Model_DbTable_Config();
		/*
	  		$db =$this->getFrontController()->getParam('bootstrap')->getResource('db');
        	$profiler = $db->getProfiler();
        	print_r($profiler->getQueryProfiles());
        	$query = $profiler->getLastQueryProfile();
        	echo $query->getQuery();*/
    }

    public function indexAction()
    {
        $this->_helper->redirector('basic');
    }

    /**
     * 基本设置
     */
    public function basicAction()
    {
        $configRowSet = $this->_configTable->fetchAll('1');
        if(!$configRowSet){
            throw new Exception("系统初始值载入错误！请与软件作者联系！xydream@gmail.com");
        }
        foreach ($configRowSet->toArray() as $value){
            $configs[$value['key']] = $value['value'];
        }
        if($this->_request->isPost()){
            $data = $this->_getAllParams();
            foreach ($configRowSet as $config){
                if(isset($data[$config->key])){
                    $config->value = $data[$config->key];
                    $config->save();
                }
            }
            $this->refreshAction();
            $this->_helper->redirector('basic');
        }
        $this->view->oldConfig = $configs;
    }

    /**
     * 设置网站公告
     */
    public function announcementAction()
    {
        $announcement = $this->_configTable->fetchRow(array("`key`=?"=>"announcement"));
        if($this->_request->isPost()){
            $announcement->value = $this->_getParam('announcement','');
            $announcement->save();
            $this->refreshAction();
            $this->_helper->redirector('announcement');
        }
        $this->view->announcement = $announcement;
    }

    /**
     * 设置会员参数
     */
    public function userAction()
    {
        $configRowSet = $this->_configTable->fetchAll('1');
        if(!$configRowSet){
            throw new Exception("系统初始值载入错误！请与软件作者联系！xydream@gmail.com");
        }
        foreach ($configRowSet->toArray() as $value){
            $configs[$value['key']] = $value['value'];
        }
        if($this->_request->isPost()){
            $data = $this->_getAllParams();
            foreach ($configRowSet as $config){
                if(isset($data[$config->key])){
                    $config->value = $data[$config->key];
                    $config->save();
                }
            }
            $this->refreshAction();
            $this->_helper->redirector('user');
        }
        $this->view->userConfig = $configs;
    }

    /**
     * 更新设置缓存
     */
    public function refreshAction()
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        $cache = $bootstrap->getResource('cache');
        $cacheId = 'config';
        $cache->remove($cacheId);
        $data = $this->_configTable->fetchAll('1')->toArray();
        foreach ($data as $value){
            $config[$value['key']] = $value['value'];
        }
        $cache->save($config,$cacheId);
        Zend_Registry::getInstance()->set('config',$config);
        $this->view->config = $config;
    }
}